package com.lipuwater.agent.ble.command;


import com.lipuwater.agent.ble.utils.CRC16M;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2016/11/28.
 * Email:924686754@qq.com
 * 0x20指令，非数字签名
 */
public class DeviceCommand20 {
    /*包头1
      包头2
      数据长度
      消息序号SN
      功能码
      起始地址低字节
      起始地址高字节
      修改数量
      地址值
      ...
      地址值
      CRC低字节
      CRC高字节*/

    /**
     * 获取滤芯复位指令、参数及状态清零等指令
     * address=0102 01 00000000000000复位一级滤芯
     *
     * @param address   参数地址  16进制  查看0x20指令表
     * @param msgNumber
     * @return
     */
    public static String getCommand(String address, String msgNumber) {
        String results = "11" + msgNumber + "20";
        String lowByte = "E8";
        String highByte = "03";
        results += (lowByte + highByte);
        String updateNumber = "0A";//修改数量
        results += updateNumber;
        results += address;
        //加上CRC校验
        byte[] sbuf = CRC16M.getSendBuf(results);
        results = CRC16M.getBufHexStr(sbuf);
        //检验数据段是否包含ff
        results = vFill(results);
        return "FFFF" + results;
    }

    /**
     * 获取反冲洗指令
     *
     * @return
     */
    public static String getIrrigation(String msgNumber) {
        String results = "11" + msgNumber + "20E8030A01010100000000000000";
        //加上CRC校验
        byte[] sbuf = CRC16M.getSendBuf(results);
        results = CRC16M.getBufHexStr(sbuf);
        return "FFFF" + results;
    }

    /**
     * 获取删除历史数据指令
     *
     * @param msgNumber
     * @param cleanCount 删除总量  10进制
     * @return
     */
    public static String getCleanHistoryDataCommand(int cleanCount, String msgNumber) {
        String results = "11" + msgNumber + "20E8030A01"; //02表示删除历史数据点
        String startAddress = Integer.toHexString(Integer.valueOf("40000"));
        String lowByte = startAddress.substring(startAddress.length() - 2, startAddress.length());
        //起始位置高字节，注意拿的是前面两个
        String highByte = startAddress.substring(0, 2);
        results = results + lowByte + highByte;
        String cleanCountStr = Integer.toHexString(cleanCount & 0xFF);
        cleanCountStr = cleanCountStr.length() == 1 ? "0" + cleanCountStr : cleanCountStr;
        results = results + cleanCountStr;
        results += "000000000000";
        //加上CRC校验
        byte[] sbuf = CRC16M.getSendBuf(results);
        results = CRC16M.getBufHexStr(sbuf);
        //检验数据段是否包含ff
        results = vFill(results);
        return "FFFF" + results;
    }

    /**
     * 获取清除暂停天数指令
     *
     * @param cleanCount
     * @param msgNumber
     * @return
     */
    public static String getCleanStopDayCommand(int cleanCount, String msgNumber) {
        String results = "11" + msgNumber + "20E8030A02"; //02表示删除历史数据点
        String startAddress = Integer.toHexString(Integer.valueOf("41000"));
        String lowByte = startAddress.substring(startAddress.length() - 2, startAddress.length());
        //起始位置高字节，注意拿的是前面两个
        String highByte = startAddress.substring(0, 2);
        results = results + lowByte + highByte;
        String cleanCountStr = Integer.toHexString(cleanCount & 0xFF);
        cleanCountStr = cleanCountStr.length() == 1 ? "0" + cleanCountStr : cleanCountStr;
        results = results + cleanCountStr;
        results += "000000000000";
        //加上CRC校验
        byte[] sbuf = CRC16M.getSendBuf(results);
        results = CRC16M.getBufHexStr(sbuf);
        //检验数据段是否包含ff
        results = vFill(results);
        return "FFFF" + results;
    }

    /**
     * 检查指令数据段是否包含FF，是的话在ff后面填充0x55
     *
     * @param command
     */
    private static String vFill(String command) {
        String results = "";
        for (int i = 0; i < command.length() - 4; i += 2) {
            if (command.substring(i, i + 2).equals("ff") || command.substring(i, i + 2).equals("FF")) {
                results += command.substring(i, i + 2) + "55";
            } else {
                results += command.substring(i, i + 2);
            }
        }
        return results + command.substring(command.length() - 4, command.length());
    }


}
